<template>
	<jk-dialog
		v-model="saveDialogState"
		:title="`${dataId ? '新增' : '编辑'}-字典`"
		:confirm-btn-loading="confirmBtnLoading"
		:show-content-loading="showContentLoading"
		:append-to-body="false"
		:modal-append-to-body="false"
		@on-confirm="onConfirmBtn"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
	>
		<el-form ref="ruleForm" :model="ruleForm" :show-message="false" :rules="rules" label-width="110px">
			<el-row>
				<el-col :span="20">
					<el-form-item label="编码：" prop="code" class="margin-bottom-10">
						<jk-input
							v-model="ruleForm.code"
							class="width-100-percent"
							placeholder="编码"
							@blur="onBlurOfCode"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="20">
					<el-form-item label="名称：" prop="name" class="margin-bottom-10">
						<jk-input v-model="ruleForm.name" class=" width-100-percent" placeholder="名称" />
					</el-form-item>
				</el-col>
				<el-col :span="20">
					<el-form-item label="排序：" prop="sortNum" class="margin-bottom-10">
						<jk-number-input v-model="ruleForm.sortNum" class="width-100-percent" />
					</el-form-item>
				</el-col>
				<el-col :span="20">
					<el-form-item label="备注：" prop="remark" class="margin-bottom-10">
						<el-input v-model="ruleForm.remark" type="textarea" :rows="2" placeholder="备注" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>

<script>
    import {
        systemBusinessDictGet,
        systemBusinessDictCreate,
        systemBusinessDictUpdate
    } from '../../../api/basicData/businessDict';
    import { validateCode, validateNull } from '../../../utils/jk/validator';
    export default {
        name: 'SaveDictDialog',
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: Number,
                default: null
            },
            activeMenuItem: {
                type: Object,
                default() {
                    return {};
                }
            }
        },
        data() {
            return {
                confirmBtnLoading: false,
                showContentLoading: false,
                dictionaryData: [],
                saveDialogState: false,
                ruleForm: {
                    id: '',
                    name: '',
                    code: '',
                    status: 1,
                    sortNum: 1,
                    remark: '',
                    category: ''
                },
                rules: {
                    name: [{ required: true, validator: validateNull, trigger: 'change' }],
                    code: [{ required: true, validator: validateCode, trigger: 'change' }]
                }
            };
        },
        watch: {
            dialogState(newVal) {
                this.saveDialogState = newVal;
                if (newVal) {
                    this.ruleForm.id = null;
                    this.getDependentData();
                }
            }
        },
        methods: {
            onBlurOfCode() {
                this.$set(this.ruleForm, 'code', this.ruleForm.code.toUpperCase());
            },
            getDetailRequest() {
                this.showContentLoading = true;
                return systemBusinessDictGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                        this.showContentLoading = false;
                    } else {
                        this.showContentLoading = false;
                    }
                });
            },
            async getDependentData() {
                this.dataId ? await this.getDetailRequest() : this.showContentLoading = false;
            },
            saveRequest() {
                this.confirmBtnLoading = true;
                this.$set(this.ruleForm, 'code', this.ruleForm.code.toUpperCase());
                const api = !this.dataId ? systemBusinessDictCreate : systemBusinessDictUpdate;
                let params = {};
                if (!this.dataId) {
                    params = {
                        status: 1,
                        name: this.ruleForm.name,
                        code: this.ruleForm.code,
                        sortNum: this.ruleForm.sortNum,
                        remark: this.ruleForm.remark,
                        categoryCode: this.activeMenuItem.code,
                        categoryId: this.activeMenuItem.id,
                        id: this.ruleForm.id
                    };
                } else {
                    params = {
                        status: 1,
                        name: this.ruleForm.name,
                        code: this.ruleForm.code,
                        sortNum: this.ruleForm.sortNum,
                        remark: this.ruleForm.remark,
                        categoryCode: this.activeMenuItem.code,
                        categoryId: this.activeMenuItem.id,
                        id: this.ruleForm.id
                    };
                }
                api(params).then(res => {
                    this.confirmBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-confirm');
                        this.$resetForm(this);
                    }
                });
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        this.saveRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    this.confirmBtnLoading = false;
                    this.$resetForm(this);
                    this.$set(this.ruleForm, 'sortNum', 1);
                }
            }
        }
    };
</script>
